package com.punai.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.constant.SysConstants;
import com.punai.common.constant.UserConstants;
import com.punai.common.core.domain.entity.SysDept;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.system.domain.SysDeptLinkDept;
import com.punai.system.mapper.SysDeptLinkDeptMapper;
import com.punai.system.mapper.SysDeptMapper;
import com.punai.system.service.PermissionService;
import com.punai.system.service.SysDeptLinkDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SysDeptLinkDeptServiceImpl extends ServiceImpl<SysDeptLinkDeptMapper,SysDeptLinkDept> implements SysDeptLinkDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SysDeptLinkDeptMapper deptLinkDeptMapper;

    @Override
    public List<SysDept> getDeptLinkList() {
        PageUtils.startPage();
        return sysDeptMapper.selectByTGroup("Y", SecurityUtils.getCompanyId());
    }

    @Override
    public List<SysDept> getDeptSelectTree(Long deptId) {

        List<Long> childrenDeptIds = permissionService.getChildrenDeptIds(deptId);

        // 需要排除一些内容 像是自己的范围
        List<SysDept> sysDepts = sysDeptMapper.selectList(
                Wrappers.<SysDept>lambdaQuery()
                        .select(SysDept::getDeptId, SysDept::getDeptName, SysDept::getParentId)
                        .eq(SysDept::getDelFlag, UserConstants.NORMAL)
                        .eq(SysDept::getCompanyId,SecurityUtils.getCompanyId())
        );

        if (CollUtil.isNotEmpty(sysDepts)) {
            // 排除掉自己的子集 因为无需选择默认带的
            sysDepts.removeIf(v -> childrenDeptIds.contains(v.getDeptId()));
        }

        return sysDepts;
    }

    @Override
    public List<Long> getDeptLinkTree(Long deptId) {
        List<SysDeptLinkDept> sysDeptLinkDepts = deptLinkDeptMapper.selectList(
                Wrappers.<SysDeptLinkDept>lambdaQuery()
                        .eq(SysDeptLinkDept::getDeptId, deptId)
        );
        return sysDeptLinkDepts.stream().map(SysDeptLinkDept::getLinkId).collect(Collectors.toList());
    }

    @Override
    public void setLinkIds(Long deptId, List<Long> linkDeptIds) {
        int delete = deptLinkDeptMapper.delete(
                Wrappers.<SysDeptLinkDept>lambdaQuery()
                        .eq(SysDeptLinkDept::getDeptId, deptId)
        );
        List<SysDeptLinkDept> dlds = new ArrayList<>();
        // 删除后新增
        for (Long linkDeptId : linkDeptIds) {
            SysDeptLinkDept dld = new SysDeptLinkDept();
            dld.setId(IdUtil.fastUUID());
            dld.setDeptId(deptId);
            dld.setLinkId(linkDeptId);
            dlds.add(dld);
        }

        if (CollUtil.isNotEmpty(dlds)) {
            this.saveBatch(dlds);
        }
    }

    @Override
    public void deleteByDeptId(Long deptId) {
        int delete = deptLinkDeptMapper.delete(
                Wrappers.<SysDeptLinkDept>lambdaQuery()
                        .eq(SysDeptLinkDept::getDeptId, deptId)
        );
    }
}
